// src/main/java/com/bookmanager/controller/BorrowHistoryController.java
package com.bookmanager.controller;

import com.bookmanager.model.BorrowRecord;
import com.bookmanager.model.User;
import com.bookmanager.service.StatisticsService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/borrow-history")
public class BorrowHistoryController extends HttpServlet {
    private StatisticsService statisticsService = new StatisticsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("../login.jsp");
            return;
        }

        String filter = request.getParameter("filter");
        List<BorrowRecord> borrowHistory;

        // 根据筛选条件获取不同的借阅记录
        if ("current".equals(filter)) {
            // 当前借阅
            borrowHistory = statisticsService.getCurrentBorrowRecords(user.getUserId());
        } else if ("returned".equals(filter)) {
            // 已归还
            borrowHistory = statisticsService.getReturnedBorrowRecords(user.getUserId());
        } else if ("overdue".equals(filter)) {
            // 已逾期
            borrowHistory = statisticsService.getOverdueBorrowRecords(user.getUserId());
        } else {
            // 全部记录
            borrowHistory = statisticsService.getAllBorrowRecords(user.getUserId());
        }

        request.setAttribute("borrowHistory", borrowHistory);
        request.getRequestDispatcher("borrow-history.jsp").forward(request, response);
    }
}